package Utils;

import Bean.FileBean;
import org.junit.Test;
import sun.security.provider.MD5;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.ParseException;
import java.util.UUID;

public class FileOperation {
//用文件proporties代替
    private  String classPath;
    private static final String defultstorePath = "/stores/";
    private File newfile;
    private File Directory;
    private Boolean isboolean;
    private String userPath;
    FileBean fileBean;
//用bean实体进行替代。
    private int id;
    private String fileType;
    private long size;
    private String fileName;
    private String serverFileName;
    private String relativePath;
    public FileOperation() {
//        第一种方式获取类的路径 D:\ProgramData\Tasks\FileIo\out\production\FileIo
        File file=new File(this.getClass().getResource("/").getPath());
        classPath=file.getParent()+defultstorePath;
        System.out.println(classPath);
/*//        第二种方式获取类的路径 D:\ProgramData\Tasks\FileIo\out\production\FileIo\Utils
        File file=new File(this.getClass().getResource("").getPath());*/
/*//        第三中方式获取类的路径 D:\ProgramData\Tasks\FileIo
        try {
            classfile = new File("");
            classPath = classfile.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
/*//        第四种方式获取类的路径，调用user.dir表示在哪里执行。
        Directory = new File(System.getProperty("user.dir"));

        classPath = Directory+defultstorePath;
        System.out.println("类路径"+classPath);*/
    }

    /**
     * 创建目录
     *
     * @throws IOException
     */
    public void createDiretory(String directory) throws IOException {
        Directory = new File(directory);
        System.out.println("生成文件的目录"+Directory.getPath());
        if (!Directory.exists()) {
            System.out.println("生成文件目录");
            Directory.mkdirs();
        } else {
            System.out.println("目录已经存在");
        }
    }

    /**
     * 服务器无法使用Scanner类，此类用来获取真实路径，返回
     * @return
     */
    public String getDownloadPath(){
       return null;
    }
    /**
     * 限制文件类型
     * @param buffix
     * @return
     */
    public boolean istType(String buffix){
        this.isboolean=false;
        String tt[] ={".jpg",".jpeg",".png",".pdf",".txt",".zip",".rar",".doc",".xlsx"};
        for (String t:tt){
            if (t.equals(buffix)){
                this.isboolean=true;
            }
        }
        return isboolean;
    }

    /**
     * 默认路径生成文件
     * @param fileName：传入文件文件名
     * @throws IOException
     */
    public File storesFilePathDefult(String defultPath,String fileName) throws IOException {
        String url=defultPath +"/" +fileName;
//        createDiretory(classPath + defultstorePath);
        System.out.println("url:"+url);
        newfile=this.storeFilePath(url);
        return newfile;
    }

    /**
     * 用户自定义存储路径,并且创建文件
     */
    public File storeFilePath(String userPath) throws IOException {
        File file = new File(userPath);
        System.out.println(file.getPath());
        String diretory=userPath.substring(0,userPath.lastIndexOf('/'));
        createDiretory(diretory);
        if (file.isFile()) {
            System.out.println("文件已存在");
        } else {
            System.out.println("创建文件");
            file.createNewFile();
        }
        return file;
    }

    /**
     * 写入数据进文件的另一种方式
     * @param file
     */
    public void writeFile(File file) {
     /*   try (OutputStream os = new FileOutputStream(file);
             OutputStreamWriter osw = new OutputStreamWriter(os);
             FileInputStream is=new FileInputStream(file);) {
            byte [] tempimg=new byte[10*1024*1024];
            int i=is.read(tempimg);
            while(i!=-1){
                os.write(tempimg, 0, tempimg.length);
                i=is.read(tempimg, 0, tempimg.length);
            }
            System.out.println("写入文件成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        try {
            BufferedImage bufferImage= ImageIO.read(file);
            ImageIO.write(bufferImage,"jpeg",newfile);
            System.out.println("imageIo写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 创建默认的存储路径，格式yyyy/MM/dd
     */
    @Test
    public void createDefultPath(){
        Dateoperation dateoperation=new Dateoperation();
        dateoperation.getYear();
        dateoperation.getMonth();
        dateoperation.getDay();
        userPath=dateoperation.getYear()+Dateoperation.SEPRTOR
                +dateoperation.getMonth()+Dateoperation.SEPRTOR
                +dateoperation.getDay();
        System.out.println(userPath);
    }

    /**
     * 创建服务器端用户名字
     */
    @Test
    public void creatServerFileName(){
        serverFileName = "/"+((fileName+System.currentTimeMillis()).hashCode() >> 1);
    }

    public FileBean fileInfo(){
        fileBean=new FileBean();
        fileBean.setId(id);
        fileBean.setFileUrl(getClassPath()+getUserPath()+getServerFileName());
        fileBean.setClassUrl(getClassPath());
        Dateoperation date =new Dateoperation();
        try {
            fileBean.setCreateTime(date.getcurrentTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //排除掉名字前的-号
        fileBean.setNameKey(getServerFileName().substring(1));
        fileBean.setName(getFileName());
        fileBean.setSize(getSize());
        fileBean.setStatus(0);
        fileBean.setType(getFileType());
        fileBean.setRelativePath(getRelativePath());
        return  fileBean;
    }

    public String getClassPath() {
        return classPath;
    }

    public String getUserPath() {
        return userPath;
    }

    public void setUserPath(String userPath) {
        this.userPath = userPath;
    }

    public void setClassPath(String classPath) {
        this.classPath = classPath;
    }

    public File getDirectory() {
        return Directory;
    }

    public void setDirectory(File directory) {
        Directory = directory;
    }

    public String getFileType() {
        return fileType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getServerFileName() {
        return serverFileName;
    }

    public void setServerFileName(String serverFileName) {
        this.serverFileName = serverFileName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRelativePath() {
        relativePath="/WEB-INF"+defultstorePath+userPath+serverFileName;
        return relativePath;
    }
}
